package com.tomek.ktoryoperator.utils;

import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.text.format.DateFormat;
import android.util.Log;

import com.tomek.ktoryoperator.C;
import com.tomek.ktoryoperator.R;
import com.tomek.ktoryoperator.adapters.MainListAdapter;
import com.tomek.ktoryoperator.model.SingleEntry;
import com.tomek.ktoryoperator.providers.ProvidersFactory;

public class DialogUtils {
	public static void showTechInfoDialog(Context context, boolean force) {
		String disclaimer = "disclaimer2";

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		boolean alreadyDisplayed = prefs.getBoolean(disclaimer, false);

		if (!alreadyDisplayed || force) {
			new AlertDialog.Builder(context).setTitle(R.string.tech_restrict_title)
					.setMessage(R.string.tech_restrict_msg).setIcon(android.R.drawable.ic_dialog_info)
					.setNeutralButton(R.string.close, null).show();

			prefs.edit().putBoolean(disclaimer, true).commit();
		}
	}

	public static void showTipsDialog(Context context) {
		String tips = "tip1";

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		boolean alreadyDisplayed = prefs.getBoolean(tips, false);

		if (!alreadyDisplayed) {
			new AlertDialog.Builder(context).setTitle(R.string.tips_title).setMessage(R.string.tips_msg)
					.setIcon(android.R.drawable.ic_dialog_info).setNeutralButton(R.string.close, null).show();

			prefs.edit().putBoolean(tips, true).commit();
		}
	}

	public static void showStatsDialog(Context context, MainListAdapter mainListAdapter) {
		Map<String, Integer> map = mainListAdapter.createStatsMap();
		int count = mainListAdapter.getAllItemsCount();
		int read = 0;

		StringBuilder sb = new StringBuilder();
		for (Entry<String, Integer> e : map.entrySet()) {
			sb.append(e.getKey());
			sb.append(" - ");
			sb.append(e.getValue());
			sb.append("\n");

			read += e.getValue();
		}

		int unread = count - read;

		String msg = String.format(context.getString(R.string.stats_msg), count, unread, count - unread, sb.toString());

		AlertDialog ad = new AlertDialog.Builder(context).setTitle(R.string.menu_stats).setMessage(msg)
				.setIcon(android.R.drawable.ic_dialog_info).setNeutralButton(R.string.close, null).create();
		ad.setCanceledOnTouchOutside(true);

		ad.show();
	}

	public static void showDetailsDialog(Context context, SingleEntry se) {
		Date d = new Date(se.date);

		String date = se.date == 0 ? context.getString(R.string.never) : String.format("%s %s", DateFormat
				.getLongDateFormat(context).format(d), DateFormat.getTimeFormat(context).format(d));

		String providerName = "";
		String numberInfo = "";

		if (se.valueId != null) {
			providerName = ProvidersFactory.getInstance().getInfoProviders().get(se.providerId).getProviderName(context);
			numberInfo = ProvidersFactory.getInstance().getInfoProviders().get(se.providerId)
					.getNumberInfoName(context, se.valueId);
		}

		String msg = String.format(context.getString(R.string.details_msg), se.abonent, se.number, date, providerName,
				numberInfo);

		AlertDialog ad = new AlertDialog.Builder(context).setTitle(R.string.details_title).setMessage(msg)
				.setIcon(android.R.drawable.ic_dialog_info).setNeutralButton(R.string.close, null).create();
		ad.setCanceledOnTouchOutside(true);

		ad.show();
	}

	public static void showAboutDialog(final Context context) {
		String version = "";
		try {
			PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
			version = pi.versionName;
		} catch (NameNotFoundException e) {
			// shouldn't happen
			Log.e(C.TAG, "onOptionsItemSelected()", e);
		}
		String msg = String.format(context.getString(R.string.about_msg), version);

		new AlertDialog.Builder(context).setTitle(R.string.about_title).setMessage(msg)
				.setIcon(android.R.drawable.ic_dialog_info)
				.setPositiveButton(R.string.email, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dlg, int sumthin) {
						context.startActivity(new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",
								context.getString(R.string.emails), null)));
					}
				}).setNeutralButton(R.string.www, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dlg, int sumthin) {
						context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(context
								.getString(R.string.www_value))));
					}
				}).setNegativeButton(R.string.close, null).show();
	}

	// public static void showTodayRefreshedInfo(Context context) {
	// new AlertDialog.Builder(context).setTitle(R.string.info_up_to_date_title)
	// .setMessage(R.string.info_up_to_date_msg).setIcon(android.R.drawable.ic_dialog_info)
	// .setNeutralButton(R.string.close, null).show();
	// }

	public static void showDeviceNotCapableWithTelephony(Context context) {
		new AlertDialog.Builder(context).setTitle(R.string.dialog_no_telephony_title)
				.setMessage(R.string.dialog_no_telephony_msg).setIcon(android.R.drawable.ic_dialog_info)
				.setNeutralButton(R.string.close, null).show();
	}
}